package controller;

import dao.GoodDao;
import dao.impl.GoodDaoImpl;
import po.Good;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UpdateGood extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
//        System.out.println("POST请求中");
//        System.out.println("req的Names" + req.getParameterNames());
//        System.out.println(req.getParameter("newGood"));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("GET请求中");
//        System.out.println(req.getParameter("name"));
        // 数据读取
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        int count = Integer.parseInt(req.getParameter("count"));
        double cost = Double.parseDouble(req.getParameter("cost"));

//        System.out.println(id);
//        System.out.println(name);
//        System.out.println(type);
//        System.out.println(count);
//        System.out.println(cost);

        // 数据持久化
        GoodDao goodDao = new GoodDaoImpl();
        Good good = goodDao.getGood(id);
        good.setGoodName(name);
        good.setType(type);
        good.setCost(count);
        good.setCost(cost);

        goodDao.updateGood(good);
    }
}
